package blog.service.impl;

import blog.entity.CateGory;
import blog.mapper.ArticleMapper;
import blog.mapper.CategoryMapper;
import blog.service.CateGoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CateGoryServiceImpl implements CateGoryService {
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private ArticleMapper articleMapper;
    @Override
    public List<CateGory> findAll() {
        List<CateGory> cateGoryList = categoryMapper.findAll();
        //每个分类所对应的文章数和子分类
        for (CateGory c:cateGoryList){
            int articleCount = articleMapper.getArticleCountOfCategory(c.getCateGoryId());
            List<CateGory> subCateGoryList = categoryMapper.getSubCateGoryListById(c.getCateGoryId());
            for(CateGory cc:subCateGoryList){
                int subarticleCount = articleMapper.getArticleCountOfCategory(cc.getCateGoryId());
                cc.setArticleCount(subarticleCount);
            }
            c.setSubCateGoryList(subCateGoryList);
            c.setArticleCount(articleCount);
        }
        return cateGoryList;
    }

    @Override
    public void insertCateGory(CateGory cateGory) {
        categoryMapper.insertCateGory(cateGory);
    }

    @Override
    public void deleteGoryById(String cateGoryId) {
        categoryMapper.deleteGoryById(cateGoryId);
    }

    @Override
    public CateGory selectGoryByName(String cateGoryName) {
        return categoryMapper.selectGoryByName(cateGoryName);
    }

    @Override
    public List<CateGory> getAllParentCateGory(Integer pid) {
        return categoryMapper.getAllParentCateGory(pid);
    }

    @Override
    public List<CateGory> getAllCateGory() {
        return categoryMapper.findAll();
    }

    @Override
    public List<CateGory> getArticleCateGory(Integer id) {
        return categoryMapper.listCateGoryRefByArticleId(id);
    }

    @Override
    public Integer getCateGoryPid(Integer id) {
        List<CateGory> cateGoryList = getArticleCateGory(id);
        int cateGoryPid = 0;
        for (CateGory c:cateGoryList){
            if(c.getCateGoryPid() == 0){
                cateGoryPid = c.getCateGoryId();
            }
        }
        return cateGoryPid;
    }

    @Override
    public Integer getCateGoryChildrenId(Integer id) {
        List<CateGory> cateGoryList = getArticleCateGory(id);
        int cateGoryChildrenId = 0;
        for (CateGory c:cateGoryList){
            if(c.getCateGoryPid() != 0){
                cateGoryChildrenId = c.getCateGoryId();
            }
        }
        return cateGoryChildrenId;
    }

    @Override
    public CateGory getCateGoryById(Integer id) {
        return categoryMapper.getCateGoryById(id);
    }

    @Override
    public void updateCateGory(CateGory cateGory) {
         categoryMapper.updateCateGory(cateGory);
    }
}
